﻿unit ScanEventFrm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,  JvFormPlacement, JvComponentBase,
  cxGridExportLink,
  dxRibbonForm,
  JvAppIniStorage, JvXPBar, JvExControls, JvXPCore, JvXPContainer, GridsEh,
  DBGridEh, ComCtrls, JvExComCtrls, JvComCtrls, ExtCtrls, JvExExtCtrls,
  JvSplit, DB, JvXPButtons, Grids,
  StdCtrls, FIBDataSet, pFIBDataSet, Menus, ImgList, FibQuery, pFIBCacheQueries,
  cxPropertiesStore, cxGraphics, cxControls,
  cxLookAndFeelPainters, dxSkinsCore, dxSkinMcSkin, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinSeven, dxSkinsdxRibbonPainter, cxClasses,
  dxRibbon, cxLookAndFeels, dxSkinOffice2010Black;

type
  TScanEventForm = class(Tdxcustomribbonform)
    Panel1: TPanel;
    Splitter1: TSplitter;
    UsersBarTItem: TJvXPBar;
    UGrid: TDBGridEh;
    DataSource: TDataSource;
    UDS: TpFIBDataSet;
    DataSource1: TDataSource;
    AllCheck: TCheckBox;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    NoteBar: TJvXPBar;
    Note: TMemo;
    UDS1: TpFIBDataSet;
    MDS: TpFIBDataSet;
    USource: TDataSource;
    MSource: TDataSource;
    Panel2: TPanel;
    Splitter2: TSplitter;
    Panel3: TPanel;
    Panel6: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    D1: TDateTimePicker;
    D2: TDateTimePicker;
    CDS: TpFIBDataSet;
    CDSTP: TFIBSmallIntField;
    CDSD: TFIBDateField;
    CDST: TFIBTimeField;
    CDSNAME: TFIBStringField;
    CDSPSUM: TFIBBCDField;
    CDSRSUM: TFIBBCDField;
    CDSSALDO: TFIBBCDField;
    CDSANSWER: TFIBStringField;
    CDSSTATE: TFIBSmallIntField;
    CDSETYPE: TFIBStringField;
    CDSRTYPE: TFIBStringField;
    CDSNOTE: TFIBStringField;
    CDSSDATE: TFIBDateTimeField;
    CDSEDATE: TFIBDateTimeField;
    CDSECODE: TFIBStringField;
    CDSACODE: TFIBStringField;
    CSource: TDataSource;
    StateIL: TImageList;
    CGrid: TDBGridEh;
    Splitter3: TSplitter;
    ENote: TMemo;
    N3: TMenuItem;
    Panel4: TPanel;
    PrincipalPanel: TPanel;
    Grid: TDBGridEh;
    prop1: TcxPropertiesStore;
    DS: TpFIBDataSet;
    DSECODE: TFIBStringField;
    DSACCCODE: TFIBStringField;
    DSDACTIVE: TFIBDateField;
    DSTACTIVE: TFIBTimeField;
    DSMASTER: TFIBStringField;
    DSADDR: TFIBStringField;
    DSADDRNAME: TFIBStringField;
    DSETYPE: TFIBStringField;
    DSENAME: TFIBStringField;
    DSUSR: TFIBIntegerField;
    DSUSRNAME: TFIBStringField;
    DSDERECT: TFIBSmallIntField;
    DSSHB: TFIBStringField;
    DSNOTE: TFIBStringField;
    rt: TdxRibbonTab;
    dxRibbon1: TdxRibbon;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure DataSourceDataChange(Sender: TObject; Field: TField);
    procedure GridKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure AllCheckClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure GridEnter(Sender: TObject);
    procedure Reopen;
    procedure D1Change(Sender: TObject);
    procedure CGridDrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumnEh; State: TGridDrawState);
    procedure CSourceDataChange(Sender: TObject; Field: TField);
    procedure N3Click(Sender: TObject);
  private
    procedure PrintData(AGrid: TDBGridEh; F: TField; Rect: TRect;
          BColor, FColor: TColor; Align: cardinal; Bold: boolean = false);
    procedure MakeUserList;
    procedure SetUser;
    procedure SetNote;
    procedure MakeFullList;
    procedure SetPrincipalPanel;
    function GetPriorCode: string;
  public
    { Public declarations }
  end;


var
  ScanEventForm: TScanEventForm;

implementation
 uses  SetGridEh, SetENoteFrm, cmUtils, DModule;


{$R *.dfm}


//---------------------------TScanEventForm-------------------------------------
procedure TScanEventForm.MakeFullList;
 var AccCode: string;
begin
 if DS.IsEmpty then AccCode:=''
               else AccCode:=DS.FN('AccCode').AsString;
 CDS.Close;
 if AccCode='' then Exit;
 CDS.ParamByName('AccCode').AsString:=AccCode;
 CDS.ParamByName('D1').AsDate:=D1.Date;
 CDS.ParamByName('D2').AsDate:=D2.Date;
 CDS.ParamByName('List').AsString:='';
 CDS.Open;
 CDS.Last;
end;

procedure TScanEventForm.AllCheckClick(Sender: TObject);
begin
 MakeUserList;
end;

procedure TScanEventForm.PrintData(AGrid: TDBGridEh; F: TField; Rect: TRect;
          BColor, FColor: TColor; Align: cardinal; Bold: boolean = false);
   var S: string;
  begin
   if F=nil then Exit;
   if F.DataType=ftTime then S:=' '+FormatDateTime('HH:MM',F.AsDateTime)
   else if F.DataType=ftBCD then
     if F.AsCurrency<>0 then S:=FormatFloat('#,##0.00',F.AsCurrency)+' '
                        else S:=''
   else S:=' '+F.AsString;
   AGrid.Canvas.Brush.Color:=BColor;
   AGrid.Canvas.Font.Color:=FColor;
   if Bold then AGrid.Canvas.Font.Style := AGrid.Canvas.Font.Style + [fsBold];
   AGrid.Canvas.FillRect(Rect);
   DrawText(AGrid.Canvas.Handle, PChar(S),Length(S),Rect,
            Align);
  end;

procedure TScanEventForm.CGridDrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumnEh; State: TGridDrawState);
 var F: TField;

 procedure PrintName;
  var TP: integer;
  begin //наименование ктивного события
   TP:=CDS.FN('TP').AsInteger;
   case TP of
    2: PrintData(CGrid, F, Rect, clWhite, clNavy, DT_LEFT, true);
    3: PrintData(CGrid, F, Rect, clInfoBk, clBlack, DT_LEFT);
    4: PrintData(CGrid, F, Rect, clCream, clMaroon, DT_LEFT);
    5: PrintData(CGrid, F, Rect, clMoneyGreen, clNavy, DT_LEFT, True);
   end;
  end;

 procedure PrintAnswer;
  var TP: integer;
  begin
   TP:=CDS.FN('TP').AsInteger;
   case TP of
    3: PrintData(CGrid, F, Rect, clInfoBk, clBlack, DT_LEFT);
    4: if F.FieldName='ANSWER' then PrintData(CGrid, F, Rect, clCream, clGreen, DT_LEFT)
                               else PrintData(CGrid, F, Rect, clCream, clMaroon, DT_LEFT);
    5: PrintData(CGrid, F, Rect, clMoneyGreen, clNavy, DT_LEFT, true);
   end;
  end;

 procedure PrintRSum;
  var TP: integer;
  begin //наименование ктивного события
   TP:=CDS.FN('TP').AsInteger;
   case TP of
    2: PrintData(CGrid, F, Rect, clWhite, clRed, DT_RIGHT, true);
    3,4,5: PrintData(CGrid, F, Rect, clAqua, clNavy, DT_RIGHT, true);
   end;
  end;

begin
 if F=nil then Exit;
 if not (gdSelected in State) or not (gdFocused in State) then //только неактивные ячейки
  begin
   F:=Column.Field;
   if (F.FieldName='NAME') then
     PrintName //печать наименованиея активного события
   else if (F.FieldName='RSUM') and (F.AsCurrency<>0) then
     PrintRSum
   else if (F.FieldName<>'TP') then PrintAnswer;
  end;
end;

procedure TScanEventForm.CSourceDataChange(Sender: TObject; Field: TField);
begin
 ENote.Text:=CDS.FN('Note').AsString;
end;

procedure TScanEventForm.D1Change(Sender: TObject);
begin
 MakeFullList;
end;

procedure TScanEventForm.DataSourceDataChange(Sender: TObject; Field: TField);
begin
 MakeUserList;
 SetNote;
 SetPrincipalPanel;
 MakeFullList;
end;

procedure TScanEventForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
 RemoveBtn(Name);
 SaveGrids(Self);
 Action:=caFree;
end;

procedure TScanEventForm.FormCreate(Sender: TObject);
begin
 AddButton(self);
 InitGrids(Self);
 LoadForm(prop1,Self.Name);
 DS.Open;
 UDS1.Open;
 MDS.Open;
end;

function TScanEventForm.GetPriorCode: string;
begin
 Result:='';
 if DS.IsEmpty then Exit;
 DS.Prior;
 if not DS.Bof then Result:=DS.FN('ECode').AsString;
 DS.Next;
end;

procedure TScanEventForm.GridEnter(Sender: TObject);
begin
 SetNote;
end;

procedure TScanEventForm.GridKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
 case Key of
  VK_RETURN: SetUser;
 end;
end;

procedure TScanEventForm.MakeUserList;
 var Code: integer;
begin
 if DS.IsEmpty then Exit;
 if not UDS.IsEmpty then Code:=UDS.FN('Code').AsInteger else Code:=0;
 UDS.Close;
 if AllCheck.Checked then UDS.ParamByName('Shb').AsString:='%'
                     else UDS.ParamByName('Shb').AsString:=DS.FN('Shb').AsString;
 UDS.Open;
 UDS.Locate('Code',Code,[]);
end;

procedure TScanEventForm.N1Click(Sender: TObject);
begin
 SetUser;
end;

procedure TScanEventForm.N2Click(Sender: TObject);
 var Form: TSetENoteForm;
begin
 if DS.IsEmpty then Exit;
 Form:=TSetENoteForm.Create(Application);
 Form.EventCode:=DS.FN('ECode').AsString;
 Form.ShowModal;
 Form.Free;
 Reopen;
end;

procedure TScanEventForm.N3Click(Sender: TObject);
 var Q: TFIBQuery;
begin
 if DS.IsEmpty then Exit;
 if Application.MessageBox('Удалить текущее мероприятие ?','',1)<>idOk then Exit;
 Q:=GetQueryForUse(dm1.trnWr, 'delete from Events where Code=:Code');
 dm1.trnWr.StartTransaction;
 try
  Q.ParamByName('Code').AsString:=DS.FN('ECode').AsString;
  Q.ExecQuery;
 finally
  if dm1.trnWr.InTransaction then dm1.trnWr.Commit;
  Q.Free;
 end;
 Reopen;
end;

procedure TScanEventForm.Reopen;
 var Code,PriorCode: string;
begin
 Code:=DS.FN('ECode').AsString;
 PriorCode:=GetPriorCode;
 DS.CloseOpen(false);
 if not DS.Locate('ECode', Code, []) then
  DS.Locate('ECode', PriorCode, []);
end;

procedure TScanEventForm.SetNote;
begin
 Note.Lines.Text:='';
 if DS.IsEmpty then Exit;
 Note.Lines.Text:=DS.FN('Note').AsString;
end;

procedure TScanEventForm.SetPrincipalPanel;
 var Q: TFIBQuery;
begin
 PrincipalPanel.Caption:='';
 if DS.IsEmpty then Exit;
 Q:=GetQueryForUse(dm1.trnRD, 'select Name from spr$Kontragent where Code='+
    '(select Kontragent from spr$Schet where Code=:AccCode)');
 try
  Q.ParamByName('AccCode').AsString:=DS.FN('AccCode').AsString;
  Q.ExecQuery;
  PrincipalPanel.Caption:=Q.FN('Name').AsString;
 finally
  Q.Free;
 end;
end;

procedure TScanEventForm.SetUser;
 var SQL: string;
begin
 if UDS.IsEmpty or DS.IsEmpty then Exit;
 SQL:='UPDATE N_ReestrData SET Usr='+UDS.FN('Code').AsString+
      ' WHERE acCode='#39+DS.FN('AccCode').AsString+#39;
 dm1.db1.Execute(SQL);
 Reopen;
end;

end.
